package com.ssc.demo2;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

public class Customer01 {
    private static final String BROKER_URL="tcp://49.234.233.179:61616";
   public static void main(String[] args){
       //连接工厂
       ConnectionFactory connectionFactory=null;
       //连接实例
       Connection connection=null;
       //收发的线程信息
       Session session=null;
       //消息发送目标地址
       Destination destination=null;
       try{
           //创建连接工厂
           connectionFactory=new ActiveMQConnectionFactory(ActiveMQConnectionFactory.DEFAULT_USER,ActiveMQConnectionFactory.DEFAULT_PASSWORD, BROKER_URL);
           //获取连接实例
           connection=connectionFactory.createConnection();
           //启动连接
           connection.start();
           //创建会话
           session =connection.createSession(Boolean.FALSE,Session.AUTO_ACKNOWLEDGE);
           //创建队列(返回一个消息目的地)
           destination=session.createTopic("test1.topic");
           //创建消息订阅者
           MessageConsumer consumer=session.createConsumer(destination);
           //消息发布者添加监听器
           consumer.setMessageListener(new MessageListener() {
               public void onMessage(Message message) {
                   try {
                       System.out.println("订阅者01接收到的消息"+((TextMessage)message).getText());
                   }catch (JMSException e){
                       e.printStackTrace();
                   }

               }
           });
       } catch (JMSException e) {
           e.printStackTrace();
       }
   }
}
